<template>
    <div class="list-page">
        <div class="list-page-header list-page-header-flex">
            <div class="flex-left">
                <el-button type="primary" @click="doUpload">导入Excel</el-button>
                <el-button type="warning" @click="doDownload">下载模版</el-button>
                <el-button type="primary" @click="doExecute">执行</el-button>
                <!-- <el-popconfirm title="删除人员后改人员无法登录系统，是否继续？" confirmButtonText='确定' cancelButtonText='取消' @confirm="del">
                    <template #reference>
                        <el-button>删除</el-button>
                    </template>
                </el-popconfirm> -->
                <input ref="excel" type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" style="display: none;" @change="onFileChange">
            </div>
            <div class="flex-right">
                <el-input v-model="query.employeeNo" class="search-input-class" placeholder="工号" />
                <el-input v-model="query.name" class="search-input-class" placeholder="姓名" />
                <el-button type="primary" @click="loadData">查询</el-button>
                <el-button @click="reset">重置</el-button>
            </div>
        </div>
        <div class="list-content">
            <el-table ref="datagrid" :data="table.data" border @row-click="handleClickTableRow" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" />
                <el-table-column prop="employeeNo" label="工号" />
                <el-table-column prop="name" label="姓名" />
                <el-table-column prop="departmentCode" label="部门编码" />
                <el-table-column prop="phone" label="电话" />
                <el-table-column prop="kindCode" label="类型" />
                <el-table-column prop="state" label="状态" :formatter="stateFormatter" />
            </el-table>
        </div>
        <div class="list-footer">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="table.pagination.currentPage" :page-sizes="table.pagination.pageSizes" :page-size="table.pagination.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="table.pagination.total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import { ref, toRefs, reactive, onMounted } from 'vue'
import { ElNotification } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
import http from '@/http.js'

export default {
    name: 'Employee',
    components: {},
    setup() {
        const excel = ref(null);
        const router = useRouter();
        const route = useRoute();
        const datagrid = ref(null);

        const state = reactive({
            query: {
                employeeNo: '',
                name: ''
            },
            table: {
                data: [],
                selected: [],
                pagination: {
                    currentPage: 1,
                    pageSizes: [10, 20, 50, 100],
                    pageSize: 10,
                    total: 0
                }
            },
            selectedEmployeeId: '',
            showInfoWindow: false
        });

        const handleSelectionChange = (val) => {
            state.table.selected = val;
        }

        const handleSizeChange = (val) => {
            state.table.pagination.pageSize = val;
            loadData();
        };

        const handleCurrentChange = (val) => {
            state.table.pagination.currentPage = val;
            loadData();
        };

        const handleClickTableRow = (row) => {
            datagrid.value.clearSelection();
            datagrid.value.toggleRowSelection(row);
        }

        const loadData = () => {
            let params = {
                employeeNo: state.query.employeeNo,
                name: state.query.name,
                page: state.table.pagination.currentPage,
                pageSize: state.table.pagination.pageSize
            }
            http.fetchForm({
                url: '/bas/employee/queryTempGrid',
                data: params
            }).then((res) => {
                state.table.data = res.data.grid;
                state.table.pagination.total = res.data.total;
            });
        }

        const stateFormatter = (row, column, cellValue, index) => {
            let data = row[column.property];
            if (data == 0) {
                return "待处理";
            } else if (data == 1) {
                return "已新增";
            } else if (data == 2) {
                return "已更新";
            } else {
                return "处理失败";
            }
        }

        const reset = () => {
            state.query.employeeNo = '';
            state.query.name = '';
            loadData();
        }

        const doUpload = () => {
            excel.value.value = '';
            excel.value.dispatchEvent(new MouseEvent('click'))
        }

        const onFileChange = () => {
            let fileObj = excel.value.files[0];
            if (fileObj) {
                let fd = new FormData();
                fd.append('file', fileObj);
                // 修改请求地址
                http.fetchFile({
                    url: '/bas/employee/importExcel',
                    method: 'post',
                    data: fd
                }).then((res) => {
                    ElNotification({
                        title: '提示',
                        message: "上传成功",
                        type: 'success'
                    });
                });
            } else {
                return;
            }
        }

        const doDownload = () => {
            let url = import.meta.env.VITE_API_URL + '/attachment/人员导入模版.xlsx';
            window.open(url, '_blank');
        }

        const doExecute = () => {
            http.fetchForm({
                url: '/bas/employee/doExecute',
                method: 'post',
                data: {
                    
                }
            }).then((res) => {
                ElNotification({
                    title: '提示',
                    message: "执行成功，请稍后查看执行结果",
                    type: 'success'
                });
            });
        }

        const del = () => {
            let selected = state.table.selected;
            if (selected.length == 0) {
                ElNotification({
                    title: '提示',
                    message: "请选择要删除的人员",
                    type: 'info'
                });
                return;
            }
            http.fetchForm({
                url: '/bas/employee/deleteById',
                method: 'post',
                data: {
                    id: selected[0].id
                }
            }).then((res) => {
                ElNotification({
                    title: '提示',
                    message: "删除成功",
                    type: 'success'
                });
                loadData();
            });
        }

        // mounted
        onMounted(() => {
            loadData();
        });

        return {
            excel,
            ...toRefs(state),
            datagrid,
            handleSelectionChange,
            handleSizeChange,
            handleCurrentChange,
            handleClickTableRow,
            loadData,
            stateFormatter,
            reset,
            doUpload,
            onFileChange,
            doDownload,
            doExecute,
            del,
        };
    }
}
</script>
<style scoped>
@import '@/assets/css/list-page.css';
</style>